<!DOCTYPE html>
<html lang="en">

	<head>
		<meta charset="UTF-8">
		<meta name="viewport" content="width=device-width, initial-scale=1.0">
		<meta http-equiv="X-UA-Compatible" content="ie=edge">
		<title>华胜后台管理</title>
		<link rel="stylesheet" type="text/css" href="css/unified.css">
		<script src="plugins/layui/layui.js"></script>
	</head>

	<body>

		<div class="kit-doc">
			<!-- 标题 -->
			<div class="kit-doc-title">
				<fieldset>
					<legend>
						<a name="blockquote">委外加工</a>
					</legend>
				</fieldset>
			</div>
			<!-- 标题 end-->

			<div class="boderbox">
				<div class="rowtop">
					<button class="layui-btn addbtn openwinbtn" data-method="addmang">添加+</button>

				</div>
				<div class="layui-tab-content notab">
					<div class="layui-tab-item layui-show">
						<table class="layui-table " lay-filter="table100" id="Etable"></table>
						
					</div>
				</div>
			</div>
			<!-- 概述 end-->
		</div>
		<script type="text/html" id="checkboxTpl">
			<input type="checkbox" name="lock" value="{{d.catalogId}}" title="重点" lay-filter="lockDemo" {{ d.recommend==1 ? 'checked' : '' }}>
		</script>

		<script id="openbox" type="text/html">
			
			<!--<table class="layui-table" id="openseetable"></table>-->
			<table class="layui-table " lay-filter="table100" id="openseetable" lay-skin="row" lay-even>
				<thead>
					<tr>
						
						<td class="stats">状态</td>
						
						<td class="barCode">条码</td>
						<td class="commodityNumber">商品编号</td>
						<td class="commodityPrice">商品价格</td>
						<td class="buyAmount">当前数量</td>
						<td class="operation">操作</td>
						<td class="returnNum">退料数量 <span class="layui-badge-dot"></span></td>
					</tr>
				</thead>
				<tbody></tbody>
			</table>
		</script>

		<script type="text/html" id="orderStateTp">
			{{# if(d.entrustState == "Waiting_for_delivery" || d.entrustState == null ){ }}
			<b style="color: #f30000;"> 等待领料</b> {{# } else if(d.entrustState == "Already_shipped"){ }}
			<b style="color: #ff9800;"> 待收货</b> {{# } else if(d.entrustState == "Success"){ }}
			<b style="color: #0aab25;"> 已领料</b> {{# } else if(d.entrustState == "Complete"){ }}
			<b style="color: #009688;"> 已确认完工</b> {{# } }}

		</script>

		<!--操作按钮菜单-->
		<script type="text/html" id="operationTp">
			{{# if(d.entrustState == "Waiting_for_delivery" || d.entrustState == null){ }}
			<a class="layui-btn " lay-event="selectThis">查看订单</a>
			{{# } else if(d.entrustState == "Already_shipped"){ }}
			<a class="layui-btn " lay-event="selectThis">查看订单</a>
			<a class="layui-btn layui-bg-blue" lay-event="YesThis">确认完工</a>
			{{# } else if(d.entrustState == "Complete"){ }}
			<a class="layui-btn " lay-event="newThis">新增商品入库</a>
			{{# } else if(d.entrustState == "Success"){ }}
			<a class="layui-btn " lay-event="OverThis">完成订单</a>
			{{# } }}

			<!--<a class="layui-btn " lay-event="selectThis" >查看</a><a class="layui-btn layui-bg-blue" lay-event="YesThis" >确认收货</a>-->

		</script>
		<script src="js/jquery-3.2.1.min.js"></script>
		<script type="text/javascript" src="js/allajax.js"></script>
		<!--<script type="text/javascript" src="js/comClass.js" ></script>-->
		<script>
		
		var tableArray = new Array();
			layui.use(['layer', 'form', 'element', 'table'], function() {
				var layer = layui.layer,
					form = layui.form,
					element = layui.element,
					table = layui.table,
					table1 = layui.table;
				entrustSelect();

				function entrustSelect() {
					 tableArray = new Array();
					AjaxPostFn('system/entrustInfo/selectEntrust?page=1&pageSize=10', "{}", function(data) {
						if(data.htmlState.status == 200) {
							$.each(data.dataResultObj, function(idx, obj) {
								var info = obj.entrustInfo;
								tableArray.push({
									"entrustNumber": info.entrustNumber,
									"entrustId": info.entrustId,
									"entrustUndertookId": info.entrustUndertookId,
									"locationId": info.locationId,
									"entrustMerchant": info.entrustMerchant,
									"entrustState": info.entrustState,
									"entrustMachining1": info.entrustMachining /100,
									"entrustAmount1": info.entrustAmount/100,
									"fullSection1": info.fullSection /100,
									"generateTime1": getMyDate(info.generateTime),
									"entrustCommodities": obj.entrustCommodities
								})
							});
						}

						var tableIns = table.render({
							elem: '#Etable',
							cols: [
								[ //标题栏
									{
										type: 'numbers',
										fixed: 'left',
										title: '行号'
									}, {
										field: 'entrustState',
										width: 150,
										title: '状态',
										templet: '#orderStateTp',
										fixed: 'left',
									}, {
										field: 'operation',
										width: 250,
										title: '操作',
										templet: '#operationTp',
										fixed: 'left'
									}, {
										field: 'entrustId',
										minWidth: '0%',
										width: '0%',
										type: 'space',
										style: 'display:none'
									}, {
										field: 'entrustNumber',
										width: 200,
										title: '委托编号'
									}, {
										field: 'entrustUndertookId',
										
										title: '经手人ID'
									}, {
										field: 'locationId',
										width: 150,
										title: '发货仓库ID'
									}, {
										field: 'entrustMerchant',
										width: 150,
										title: '门店id'
									}, {
										field: 'entrustMachining1',
										width: 150,
										title: '加工费',
									}, {
										field: 'entrustAmount1',
										width: 150,
										title: '委托金额'
									}, {
										field: 'fullSection1',
										width: 150,
										title: '全款'
									}, {
										field: 'generateTime1',
										width: 150,
										title: '发布时间'
									}

								]
							],
							data: tableArray,
							id: 'Etable',
							size: 'lg',
							height: 'full-300',
							even: true,
							page: true, //是否显示分页
							done: function() {
								//图片点击放大
								TopimgFn();
							}
						});
					})
				} //select结束
				
				
				
				function comEnFn(thisjson){
					var tableCom  = table.render({
						elem: '#openseetable',
						cols: [
							[ //标题栏
								{type: 'entrustNumber',minWidth: '0%',width: '0%',type: 'space',style: 'display:none'}, 
								{type: 'stats',title: '状态'}, 
								{type: 'barCode',title: '条码'}, 
								{type: 'commodityNumber',title: '商品编号'}, 
								{type: 'commodityPrice',title: '商品价格'}, 
								{type: 'buyAmount',title: '出库数量'}, 
								{type: 'operation',title: '操作'}, 
								{type: 'returnNum',title: '退料数量'}, 
							]
						],
						id: 'openseetable',
						data: thisjson,
						done: function() {
							console.log(thisjson)
						}
					});
				}

				table.on('tool(table100)', function(obj) {
					var data = obj.data;

					if(obj.event === 'selectThis') {
						layer.open({
							type: 1,
							skin: 'layui-layer-molv',
							title: '查看委外订单号:' + data.entrustNumber + ' 订单',
							maxmin: 'true',
							shade: 0,
							area: ['80%', '80%'],
							content: $("#openbox").html(),
							skin: 'demo-class',
							btn: ["确认发货", "关闭"],
							yes: function(index, layero) {
								//按钮1的回调

								console.log(JSON.stringify(data));
								if(data.entrustState == 'Already_shipped') {
									layer.msg("该订单商品已经出库了！");
								} else {
									$.each(data.entrustCommodities, function(voidx, voobj) {
										var insertjson ={ 
											"barCode": voobj.barCode,
											"theNum": voobj.buyAmount,
											"locationId": data.locationId,
											"status": 0,
											"createId": 1,
											"createTime": new Date().getTime(),
											"commodityNumber": voobj.commodityNumber,
											"merchantNumber": data.entrustMerchant
										}
//										var insertjson = '{ "barCode": "' + voobj.barCode + '","theNum": ' + voobj.buyAmount + ', "locationId": ' + data.locationId + ', "status": 0,"createId": 1, "createTime": "' + new Date().getTime() + '", "commodityNumber": "' + voobj.commodityNumber + '", "merchantNumber": ' + data.entrustMerchant + '}   ';

										AjaxPostFn('system/repository/prepare/insert', JSON.stringify(insertjson), function(data1) {
											if(data1.htmlState.status == 200) {
												layer.msg("订单出货成功");
												$("#opensee tbody tr:eq(" + voidx + ") td:eq(0)").html("<b style='color: #009688;'>出库成功</b>");
												$("#opensee tbody tr:eq(" + voidx + ") td:eq(0)").attr("data-stats", "1");
											} else {
												$("#opensee tbody tr:eq(" + voidx + ") td:eq(0)").html("<b style='color: #f30000;'>出库异常</b>");
											}
										}, voidx);
									
									})
									var enjson =  {
											  "entrustInfo":{
											    "entrustNumber":data.entrustNumber,
												"entrustState":"Already_shipped"
											    }
											}
									setTimeout(function(){
										if($("#opensee tbody tr:eq(0) td:eq(0)").attr("data-stats") == 1){
											AjaxPostFn('system/entrustInfo/modifyEntrust',JSON.stringify(enjson), function(data1) {
												//修改委外加工单接口
												
												
											})
										}
										
										
									},1000)
								}

							},
							btn2: function(index, layero) {
								//按钮2的回调
							},
							cancel: function() {
								//右上关闭的回调

							},
							success: function() {
								//渲染商品数据
								
//								comtableArray  = [];
//								$.each(data.entrustCommodities, function(voidx, voobj) {
//									
//									comtableArray.push({
//						                "entrustNumber":data.entrustNumber,
//						                "operation": '<a class="layui-btn layui-btn-danger " lay-event="returnthis" >点击退料</a>',
//						                "stats":'..',
//						                "barCode":voobj.barCode,
//						                "commodityNumber": voobj.commodityNumber,
//						                "commodityPrice": voobj.commodityPrice,
//						                "buyAmount":voobj.buyAmount,
//						                "returnNum":'0',
//					              })
//								});
								var Enjson = '' ;
								
								$.each(data.entrustCommodities, function(voidx, vobj) {
									if(data.entrustState == 'Already_shipped'){
//										已经出库
										Enjson +='<tr><td data-stats="1"><b style="color: #0aab25;"> 已出库</b></td> '
									}else if(data.entrustState == 'Waiting_for_delivery  '){
										Enjson +='<tr><td data-stats="0"><b style="color: #f30000;"> 等待出库</b></td>  '
									}else{
										Enjson +='<tr><td data-stats="0"><b style="color: #f30000;"> </b></td>  '
									}
									
									Enjson +="<td class='barCode'>"+vobj.barCode+"</td>"+
										"<td class='commodityNumber'>"+vobj.commodityNumber+"</td>"+
										"<td>"+vobj.commodityPrice/100+"</td>"+
										"<td class='buyAmount'>"+vobj.buyAmount+"</td>"+
										"<td><input type='button' value='点击进行退料' data-locationId='"+data.locationId+
										"' data-merchantNumber='"+data.entrustMerchant+"' data-entrustNumber='"+data.entrustNumber+
										"' data-entrustDetailedid='"+vobj.entrustDetailedid+
										"' data-entrustState='"+data.entrustState+
										"' class='layui-btn addbtn returnEn'></td>"+
										"<td><input type='text' class='layui-input  intheNum '></td></tr>";
								})
								
								$("#openseetable tbody ").html(Enjson)
								
								$(".returnEn").click(function(){
									var $index =  $(".returnEn").index($(this));//获取当前按钮的索引值
									var table = $("#openseetable tbody tr:eq("+$index+")"); //当前行
									
									if( table.find(".intheNum").val() == '' ){
										layer.msg("请输入退料数量!")
									
									}else{
										var innum = parseInt( table.find(".intheNum").val());
										var oldNum = parseInt( table.find(".buyAmount").html());
										
										if(oldNum < innum){
											layer.msg("请输入恰当的数字!");
											return false;
										}
										var newNum = oldNum - innum ; 
										table.find(".buyAmount").html(newNum);
										
										console.log()
										
										if($("#openseetable tbody tr:eq("+$index+") td:eq(0)" ).attr("data-stats") == 1){
											//已经出库的状态.需要将库存追加回去
											var instockjson ={ 
												"batchId":"1",
												"barCode":table.find(".barCode").html(),
												"theNum": table.find(".intheNum").val(),
												"locationId": table.find(".returnEn").attr('data-locationId'),
	//											"status": 0,
												"updateId": 1,
												"updateTime": new Date().getTime(),
	//											"commodityNumber": voobj.commodityNumber,
												"merchantNumber": table.find(".returnEn").attr('data-merchantNumber'),
												"comment":"委外加工订单退料入库"
											}
											
											AjaxPostFn('system/repository/in/insert', JSON.stringify(instockjson), function(data1) {
												if(data1.htmlState.status == 200) {
												
													
												} else {
													
												}
											});
										
											
										}
											
										
										var Enoutjson ={
											  "entrustInfo":{
											    "entrustNumber":table.find(".returnEn").attr('data-entrustNumber'),
											    "entrustState":table.find(".returnEn").attr('data-entrustState')
											   },
											    "commodity": [
									            {
									                "entrustNumber":table.find(".returnEn").attr('data-entrustNumber'),
									                "commodityNumber": table.find(".commodityNumber").html(),
									                "entrustDetailedid":table.find(".returnEn").attr('data-entrustDetailedid'),
									                "buyAmount": table.find(".intheNum").val(),
									                "barCode": table.find(".barCode").html(),
									            }]
									    }
										
										console.log(Enoutjson)
										
										AjaxPostFn('system/entrustInfo/modifyEntrust',JSON.stringify(Enoutjson), function(data1) {
											//修改委外加工单接口
											if(data1.htmlState.status == 200) {
												layer.msg("退料成功");
												
											} else {
												
											}
											
										})
										
									
									}
									
								
								
									
								})

							}

						});
					}else if(obj.event === 'YesThis') {
						//确认完工
						var Enoutjson ={
							  "entrustInfo":{
							    "entrustNumber":data.entrustNumber,
							    "entrustState":'Complete'
							   }
   
					    }
						
						AjaxPostFn('system/entrustInfo/modifyEntrust',JSON.stringify(Enoutjson), function(data1) {
							//委外加工单接口
							if(data1.htmlState.status == 200) {
								layer.msg("修改状态成功");
								
							} else {
								
							}
							
						})
					}else if(obj.event === 'newThis'){
						
					}
				})

				$('.openwinbtn').on('click', function() {
					var othis = $(this),
						method = othis.data('method');
					active[method] ? active[method].call(this, othis) : '';
				});

				var active = {
					addmang: function() {
						var addindex = layer.open({
							type: 2,
							title: "新建委外加工单",
							area: ['100%', '100%'],
							maxmin: true,
							shade: 0,
							content: 'Entrust_in.html',
							cancel: function() {
								entrustSelect();
							}
						});

					}
				}

			})
		</script>
	</body>

</html>